package com.chenshu.shop.controller.admin;

import com.chenshu.shop.model.User;
import com.chenshu.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-06
 * Time：08:45
 */
@Controller
@RequestMapping("/admin")
public class UserController_admin {
    @Autowired
    UserService userService;
    /**
     * 在user_list.jsp显示所有用户
     * @param request
     * @return
     */
    @RequestMapping("/userList")
    public String getAllUsers(HttpServletRequest request) {
        request.setAttribute("flag", 4);
        List<User> userList = userService.getAllUsers();
        request.setAttribute("userList", userList);
        return "forward:/admin/user_list.jsp";
    }

    /**
     * 重置用户密码
     * @param request
     * @param id
     * @param password
     * @return
     */
    @RequestMapping("/userReset")
    public String resetUser(HttpServletRequest request, Integer id, String password) {
        request.setAttribute("flag", 4);
        List<User> userList = userService.updateUser(id, password);
        request.setAttribute("userList", userList);
        return "forward:/admin/user_list.jsp";
    }

    /**
     * 跳转到用户信息编辑页并且回显信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/userEdit")
    public String editUser(HttpServletRequest request, Integer id) {
        request.setAttribute("flag", 4);
        User user = userService.getUserById(id);
        request.setAttribute("msg", "正在修改用户信息");
        request.setAttribute("user", user);
        return "forward:/admin/user_edit.jsp";
    }

    /**
     * 更新用户信息
     * @param request
     * @return
     */
    @RequestMapping("/userUpdate")
    public String updateUserInfo(HttpServletRequest request, User user) {
        request.setAttribute("flag", 4);
        List<User> userList = userService.updateUserInfo(user);
        request.setAttribute("userList", userList);
        return "forward:/admin/user_list.jsp";
    }

    /**
     * 删除用户
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/userDelete")
    public String deleteUser(HttpServletRequest request, Integer id) {
        request.setAttribute("flag", 4);
        List<User> userList = userService.deleteUser(id);
        request.setAttribute("userList", userList);
        return "forward:/admin/user_list.jsp";
    }

    /**
     * 跳转到新增用户页面
     * @return
     */
    @RequestMapping("/user_add")
    public String addUser() {
        return "redirect:/admin/user_add.jsp";
    }

    /**
     * 新增用户
     * @return
     */
    @RequestMapping("/userSave")
    public String saveUser(HttpServletRequest request, User user) {
        request.setAttribute("flag", 4);
        List<User> userList = userService.saveUser(user);
        request.setAttribute("userList", userList);
        return "forward:/admin/user_list.jsp";
    }
}
